<?php
/*
 * Created on 29.01.2006
 * 
 * config:
 * stylesheet       XSL stylesheet to use for transformation.
 * attribute        Attribute (captureAttribute) with the XML data.
 * parameters       Parameters (comma separated list of parameter names) which should 
 *                  be replaced in the XSL File (##paramname##) (optional)
 * <parameter>      Parameters which are declared in 'parameters'
 */

m2import('phpinchains/Filter.php');

class XSLTFilter extends phpinchains_Filter
{
    
    var $config;
 
    function doFilter (& $env, & $chain)
    {
        if (!$GLOBALS['env']->is_new || version_compare(phpversion(), "5.0.0", "<"))
        {
            if (!function_exists("xslt_create") || !function_exists("xslt_process"))
            {
                print "No XSLT possible. Please install the XSLT extension.";
            	return;
            }
            $parameters = array();
            if ($this->config['parameters'])
            {
                $params = split(',', $this->config['parameters']);
                foreach ($params as $param)
                {
                    $param = trim($param);
                    // set parameter supported everywhere?
                    //$xslcode = str_replace('##'.$param.'##', $this->config[$param], $xslcode);
                    $parameters[$param] = $this->config[$param];
                }
            }        
            $processor = xslt_create();
            $arguments = array ();
            $arguments['/_xml'] = $env->getAttribute($this->config['attribute']);
            print xslt_process($processor, 'arg:/_xml', $this->config['stylesheet'], NULL, $arguments, $parameters);
        }
        else
        {
            if (!class_exists("XSLTProcessor"))
            {
                print "<b>No XSLT possible. Please install the XSLT extension.</b><br/>";
                return;
            }
        	$xml = new DOMDocument();
            $xml->loadXML($env->getAttribute($this->config['attribute']));
            $xsl = new DOMDocument();
            $processor = new XSLTProcessor();
            if ($this->config['parameters'])
            {
                $params = split(',', $this->config['parameters']);
                foreach ($params as $param)
                {
                    $param = trim($param);
                    // set parameter supported everywhere?
                    //$xslcode = str_replace('##'.$param.'##', $this->config[$param], $xslcode);
                    $processor->setParameter('xsl', $param, $this->config[$param]);
                }
            }        
            if (!file_exists($this->config['stylesheet'])) {
            	print "<b>XSL-Stylesheet not found: ".$this->config['stylesheet']."<b/><br/>";
            	return;
            }
            $xsl->load($this->config['stylesheet']);
            $processor->importStyleSheet($xsl);
            print $processor->transformToXML($xml);
        }
        $chain->doFilter($env);
    }
    
    function init (& $config)
    {
        $this->config = & $config;
        if (!$this->config['stylesheet'])
            print "XSLTFilter: attribute 'stylesheet' ist required<br/>";
        if (!$this->config['attribute'])
            print "XSLTFilter: attribute 'attribute' ist required<br/>";
    }

}
?>